var express = require('express');
var app = express();
var morgan = require('morgan');
var bodyParser = require('body-parser');
// var port = process.env.PORT || 8080; // 设置启动端口
// var cookieParser = require('cookie-parser');
// var session = require('express-session');
// var sessionStore  = require('session-file-store');
var mongodb = require('./config/mongodb');
var db = mongodb();
var routes = require('./routes/index');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

// var multipart = require('connect-multiparty');
// var multipartMiddleware = multipart();
app.use(express.static('dist'));
app.use(morgan('dev'));

// 跨域请求
var allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:4200');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Credentials','true');
  next();
};

app.use(allowCrossDomain);


// ------------------------接口调试
// app.get('/', function (req, res) {
//   var mockData = {
//     "results":
//     {
//       "id": 111,
//       "name":"从来不打怪兽",
//       "house":1,
//       "city":"深圳",
//       "star":1000,
//       "img":"./them/images/45282.jpg"
//     }
//   };
//
//   res.send(mockData);
//
// });


routes(app);

app.post('/host', function (req, res) {
  res.send(data);
  console.log(req.body);
  console.log("收到");

});


var server = app.listen(8081, function () {
  var port = server.address().port;
  console.log("server is already start"+port)
});
module.exports = app;
